package com.carbeauty.common.exception;

/**
 * 业务异常类
 * 
 * 用于处理业务逻辑中的异常情况:
 * - 业务规则验证失败
 * - 数据状态不符合要求
 * - 操作权限不足
 * - 业务流程错误
 * 
 * @author CarBeauty Team
 */
public class BusinessException extends RuntimeException {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 异常代码
     */
    private String code;
    
    /**
     * 异常消息
     */
    private String message;
    
    /**
     * 异常数据
     */
    private Object data;
    
    // ==================== 构造方法 ====================
    
    public BusinessException(String message) {
        super(message);
        this.message = message;
        this.code = "BUSINESS_ERROR";
    }
    
    public BusinessException(String code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }
    
    public BusinessException(String code, String message, Object data) {
        super(message);
        this.code = code;
        this.message = message;
        this.data = data;
    }
    
    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        this.message = message;
        this.code = "BUSINESS_ERROR";
    }
    
    public BusinessException(String code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.message = message;
    }
    
    // ==================== Getter/Setter ====================
    
    public String getCode() {
        return code;
    }
    
    public void setCode(String code) {
        this.code = code;
    }
    
    @Override
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public Object getData() {
        return data;
    }
    
    public void setData(Object data) {
        this.data = data;
    }
    
    // ==================== 常用业务异常静态方法 ====================
    
    /**
     * 客户相关异常
     */
    public static class Customer {
        
        public static BusinessException notFound(Long customerId) {
            return new BusinessException("CUSTOMER_NOT_FOUND", 
                "客户不存在，ID: " + customerId);
        }
        
        public static BusinessException phoneExists(String phone) {
            return new BusinessException("CUSTOMER_PHONE_EXISTS", 
                "手机号已存在: " + phone);
        }
        
        public static BusinessException cardNoExists(String cardNo) {
            return new BusinessException("CUSTOMER_CARD_EXISTS", 
                "会员卡号已存在: " + cardNo);
        }
        
        public static BusinessException insufficientBalance(String balance, String amount) {
            return new BusinessException("INSUFFICIENT_BALANCE", 
                "余额不足，当前余额: " + balance + "，消费金额: " + amount);
        }
        
        public static BusinessException insufficientPoints(Integer points, Integer required) {
            return new BusinessException("INSUFFICIENT_POINTS", 
                "积分不足，当前积分: " + points + "，所需积分: " + required);
        }
    }
    
    /**
     * 用户相关异常
     */
    public static class User {
        
        public static BusinessException notFound(Long userId) {
            return new BusinessException("USER_NOT_FOUND", 
                "用户不存在，ID: " + userId);
        }
        
        public static BusinessException usernameExists(String username) {
            return new BusinessException("USERNAME_EXISTS", 
                "用户名已存在: " + username);
        }
        
        public static BusinessException phoneExists(String phone) {
            return new BusinessException("USER_PHONE_EXISTS", 
                "手机号已存在: " + phone);
        }
        
        public static BusinessException emailExists(String email) {
            return new BusinessException("USER_EMAIL_EXISTS", 
                "邮箱已存在: " + email);
        }
        
        public static BusinessException passwordIncorrect() {
            return new BusinessException("PASSWORD_INCORRECT", 
                "密码错误");
        }
        
        public static BusinessException accountDisabled() {
            return new BusinessException("ACCOUNT_DISABLED", 
                "账号已被禁用");
        }
        
        public static BusinessException accessDenied() {
            return new BusinessException("ACCESS_DENIED", 
                "访问权限不足");
        }
    }
    
    /**
     * 订单相关异常
     */
    public static class Order {
        
        public static BusinessException notFound(Long orderId) {
            return new BusinessException("ORDER_NOT_FOUND", 
                "订单不存在，ID: " + orderId);
        }
        
        public static BusinessException statusError(String currentStatus, String targetStatus) {
            return new BusinessException("ORDER_STATUS_ERROR", 
                "订单状态错误，当前状态: " + currentStatus + "，目标状态: " + targetStatus);
        }
        
        public static BusinessException cannotCancel(String status) {
            return new BusinessException("ORDER_CANNOT_CANCEL", 
                "订单无法取消，当前状态: " + status);
        }
        
        public static BusinessException alreadyPaid() {
            return new BusinessException("ORDER_ALREADY_PAID", 
                "订单已支付，无法重复支付");
        }
    }
    
    /**
     * 材料相关异常
     */
    public static class Material {
        
        public static BusinessException notFound(Long materialId) {
            return new BusinessException("MATERIAL_NOT_FOUND", 
                "材料不存在，ID: " + materialId);
        }
        
        public static BusinessException codeExists(String code) {
            return new BusinessException("MATERIAL_CODE_EXISTS", 
                "材料编码已存在: " + code);
        }
        
        public static BusinessException insufficientStock(String materialName, String stock, String required) {
            return new BusinessException("INSUFFICIENT_STOCK", 
                "材料库存不足，材料: " + materialName + "，当前库存: " + stock + "，所需数量: " + required);
        }
        
        public static BusinessException expired(String materialName, String expiryDate) {
            return new BusinessException("MATERIAL_EXPIRED", 
                "材料已过期，材料: " + materialName + "，过期日期: " + expiryDate);
        }
    }
    
    /**
     * 服务相关异常
     */
    public static class Service {
        
        public static BusinessException notFound(Long serviceId) {
            return new BusinessException("SERVICE_NOT_FOUND", 
                "服务项目不存在，ID: " + serviceId);
        }
        
        public static BusinessException nameExists(String name) {
            return new BusinessException("SERVICE_NAME_EXISTS", 
                "服务项目名称已存在: " + name);
        }
        
        public static BusinessException priceInvalid(String price) {
            return new BusinessException("SERVICE_PRICE_INVALID", 
                "服务价格无效: " + price);
        }
    }
    
    /**
     * 数据验证异常
     */
    public static class Validation {
        
        public static BusinessException required(String fieldName) {
            return new BusinessException("FIELD_REQUIRED", 
                fieldName + "不能为空");
        }
        
        public static BusinessException invalid(String fieldName, String value) {
            return new BusinessException("FIELD_INVALID", 
                fieldName + "格式无效: " + value);
        }
        
        public static BusinessException tooLong(String fieldName, int maxLength) {
            return new BusinessException("FIELD_TOO_LONG", 
                fieldName + "长度不能超过" + maxLength + "个字符");
        }
        
        public static BusinessException tooShort(String fieldName, int minLength) {
            return new BusinessException("FIELD_TOO_SHORT", 
                fieldName + "长度不能少于" + minLength + "个字符");
        }
    }
}
